﻿Option Strict On
Option Explicit On
Option Infer On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml.Linq
Imports GenDotNet.Task.Common
Imports GenDotNet.StoredProcModel
Imports GenDotNet.Model.Common
Imports <xmlns:orm="http://kadgen.com/KADORM.xsd">
Imports <xmlns:kd="http://kadgen.com/Special.xsd">


Public MustInherit Class BaseData
   Implements IBaseData

   Private mElement As XElement
   Private mRootData As RootData
   Private mGuessesMade As Boolean

   Protected Sub New(ByVal rootData As RootData, ByVal element As XElement)
      Me.New(rootData, element, False)
   End Sub

   Protected Sub New(ByVal rootData As RootData, ByVal element As XElement, ByVal guessesMade As Boolean)
      Me.mRootData = rootData
      Me.mElement = element
      Me.mGuessesMade = guessesMade
   End Sub

   Protected Friend ReadOnly Property Element() As XElement
      Get
         Return mElement
      End Get
   End Property

   Public ReadOnly Property RootData() As RootData
      Get
         Return mRootData
      End Get
   End Property

   Public ReadOnly Property GuessesMade() As Boolean Implements Model.Common.IBaseData.GuessesMade
      Get
         Return mGuessesMade
      End Get
   End Property

   Public Function GetCustomData(ByVal name As String) As String Implements Model.Common.IBaseData.GetCustomData
      Dim attr = mElement.Attribute(name)
      If attr Is Nothing Then
         Return Nothing
      End If
      Return attr.Value
   End Function

   Protected Function GetTable(ByVal name As String) As ITableData
      Dim ret = RootData.GetTable(name)
      If ret Is Nothing Then
         Dim node = (From n In Me.Element.Document.Root...<orm:BuildTable> Where n.@OriginalName = name).FirstOrDefault
         If node Is Nothing Then
            ret = New TableData(RootData, node.@Schema, name)
            RootData.AddNewTable(ret)
         Else
            node = (From n In Me.Element.Ancestors Where n.Name.LocalName = "SPSet").FirstOrDefault...<orm:BuildRecordset>.<orm:BuildTable>.FirstOrDefault
            ret = New TableData(RootData, node.@Schema, name)
            RootData.AddNewTable(ret)
         End If
      End If
      Return ret
   End Function

   'Protected Shared Sub LoadData(ByVal element As XElement)
   '   mEntities = New Dictionary(Of String, EntityData)
   '   Dim root = element.Document.Root
   '   For Each entityElement In root...<orm:SPSet>
   '      mEntities.Add(entityElement.@Name, EntityData.LoadElement(entityElement))
   '   Next
   'End Sub

   'Protected Shared ReadOnly Property Entities() As Dictionary(Of String, EntityData)
   '   Get
   '      Return mEntities
   '   End Get
   'End Property

   'Public Shared Function GetEntity(ByVal name As String) As IEntityData
   '   Dim ret As EntityData = Nothing
   '   mEntities.TryGetValue(name, ret)
   '   Return ret
   'End Function

End Class
